package com.sgcc.ln.pmos.e_ln_mall.adapter;


import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.e_ln_mall.bean.ElectricityProportionDataBean;

import java.util.List;

public class ElectricityProportionDataAdapter extends BaseQuickAdapter<ElectricityProportionDataBean, BaseViewHolder> {

    private Context mContext;
    private boolean isHintDeviation;
    private OnEditInputCallBack onEditInputCallBack;

    public ElectricityProportionDataAdapter(Context context, List<ElectricityProportionDataBean> dataBeanList, boolean isHintDeviation) {
        super(R.layout.item_electricity_proportion_data, dataBeanList);
        this.mContext = context;
        this.isHintDeviation = isHintDeviation;
    }


    public void setOnEditInputCallBack(OnEditInputCallBack onEditInputCallBack) {
        this.onEditInputCallBack = onEditInputCallBack;
    }

    public interface OnEditInputCallBack {

        void onCurve();


    }


    @Override
    protected void convert(BaseViewHolder baseViewHolder, ElectricityProportionDataBean bean) {
        baseViewHolder.setText(R.id.tvKey, bean.getPosition());
        EditText etValue = baseViewHolder.getView(R.id.etValue);
        EditText etRatio = baseViewHolder.getView(R.id.etRatio);

        etValue.setText(bean.getCurve());

        etRatio.setVisibility(isHintDeviation ? View.GONE : View.VISIBLE);

        etRatio.setText(bean.getDeviationCurve());

        etValue.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    etValue.addTextChangedListener(textWatcher);
                } else {
                    etValue.removeTextChangedListener(textWatcher);
                    onEditInputCallBack.onCurve();
                }
            }

            TextWatcher textWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    bean.setCurve(editable.toString().trim());
                }
            };
        });

        etRatio.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    etRatio.addTextChangedListener(textWatcher1);
                } else {
                    etRatio.removeTextChangedListener(textWatcher1);
                }
            }

            TextWatcher textWatcher1 = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    bean.setDeviationCurve(editable.toString().trim());
                }
            };
        });


    }

}
